package com.lanou.tipsy.adapter.singer;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.ImageLoader;
import com.facebook.drawee.view.SimpleDraweeView;
import com.lanou.tipsy.R;
import com.lanou.tipsy.entity.singer.singers_mv.SMVData;
import com.lanou.tipsy.tools.volley.VolleySingleton;

import java.util.ArrayList;

/**
 * Created by FengJun on 15/10/22.
 * 歌手-歌手详情-mv适配器
 */
public class SingerMVAdapter extends BaseAdapter {
    private ArrayList<SMVData> datas;
    private Context context;
    private LayoutInflater inflater;
    private ImageLoader imageLoader;
    private SMVData data;

    public SingerMVAdapter(ArrayList<SMVData> datas, Context context) {
        this.datas = datas;
        this.context = context;
        inflater = LayoutInflater.from(context);
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_singer_mv, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_imageview = (SimpleDraweeView) convertView.findViewById(R.id.iv_mv_picture);
            viewHolder.tv_singer_name = (TextView) convertView.findViewById(R.id.tv_mv_singer_name);
            viewHolder.tv_songs_name = (TextView) convertView.findViewById(R.id.tv_mv_songs_name);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        data = datas.get(position);
        viewHolder.tv_singer_name.setText(data.getSingerName());
        viewHolder.tv_songs_name.setText(data.getSongName());
        String url = data.getPicUrl();
        if (url != null && url != "") {
            Uri uri = Uri.parse(url);
            viewHolder.iv_imageview.setImageURI(uri);
        } else {
            Toast.makeText(context, "木有这个MV", Toast.LENGTH_SHORT).show();
        }
        return convertView;
    }

    private class ViewHolder {
        private TextView tv_singer_name;
        private TextView tv_songs_name;
        private SimpleDraweeView iv_imageview;
    }
}
